# -*- coding: utf-8  -*-
#    OKBot Library - A Python module that uses Pywikipediabot framework used basically by OKbot on Wikimedia projects.
#    Copyright (C) 2008 OsamaK
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    Please report bugs or help imporving this program by connecting to <OsamaK.WFM@gmail.com>
import wikipedia
import simplejson

def put_page(site, page, text, comment, minor_edit = False): # (C) 2008 bjweeks, GPL 3.0 or higher.
  if page.canBeEdited() and page.botMayEdit('OKBot'):
    try:
      page.put(text, comment, None, minor_edit)
      return True
    except wikipedia.EditConflict:
      wikipedia.output(u'Edit conflict on page: %s. Skipping.' % page.title())
      return "conflict"
    except wikipedia.LockedPage:
      wikipedia.output(u'Locked page: %s. Skipping.' % page.title())
      return "locked"
    except wikipedia.SpamfilterError:
      wikipedia.output(u'Spam filter error on: %s. Skipping.' % page.title())
      return "spam"
    except wikipedia.PageNotSaved:
      wikipedia.output(u'Page not saved on: %s. Skipping.' % page.title())
    except wikipedia.NoPage:
      wikipedia.output(u'No Page on %s' % page.title())
  
  else:
    return "noneditable"

def getAPI(site, predata): # (C) 2008 Betacommand, MIT License
  while True:
    try:
      response, json = site.postForm(site.apipath(), predata)
    except wikipedia.ServerError, e:
      wikipedia.output(u'Warning! %s: %s' % (site, e))
      continue
    try:
      data = simplejson.loads(json)
      return data
    except:
      continue
